package cn.tedu._06notice.controller;

import cn.tedu._06notice.base.response.Result;
import cn.tedu._06notice.mapper.NoticeMapper;
import cn.tedu._06notice.pojo.dto.NoticeAddParam;
import cn.tedu._06notice.pojo.dto.NoticeListQuery;
import cn.tedu._06notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._06notice.pojo.entity.Notice;
import cn.tedu._06notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._06notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author tune_the last
 * @date 2025/11/11 - 16:23
 * RestController注解：
 *  1.组合注解：等价于 @Controller + @ResponseBody
 *  2.简化开发：在类上添加该注解后，会为每个方法自动添加@responseBody注解
 *
 *  Slf4j注解：
 *  1.Lombok提供的日志注解
 *  2.添加在类上，会为当前类创建一个日志对象log，调用日志对象log的方法来打印日志信息
 *  日志对象log:
 *  log.trace("日志信息");
 *  log.debug("日志信息");
 *  log.info("日志信息");
 *
 *  Knife4j相关注解
 *  1.@Tag(name="")：控制器类名在Knife4j文档中的显示名称
 *  2.@Operation(summary="")：控制器方法名在Knife4j文档中的显示名称
 *  3.@ApiOperationSupport(order=数字)：方法的顺序，数字越小优先级越高
 *  4.@Schema(description="",required=true, example="")：封装参数属性描述
 *  ...
 */
//@Controller
//@ResponseBody
//
@Tag(name = "01-资讯管理模块")
@Slf4j
@RestController
@RequestMapping("/v1/notice")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 添加资讯功能
     * @param noticeAddParam
     * @return
     */
    @Operation(summary = "添加资讯")
    @ApiOperationSupport(order = 10)
    @PostMapping("/add")
    @Parameters({
            @Parameter(name = "title", description = "资讯标题"),
            @Parameter(name = "type", description = "资讯类型"),
            @Parameter(name = "content", description = "资讯内容"),
            @Parameter(name = "status", description = "资讯状态"),
            @Parameter(name = "noticeAddParam", hidden = true)}
    )
    public Result addNotice( NoticeAddParam noticeAddParam){
        log.debug("notice = " + noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam, notice);
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        notice.setIsDelete(0);
        noticeMapper.insertNotice(notice);
        log.debug("新增后：" + notice);
        return Result.ok();
    }

    /**
     * 2.查询资讯列表
     * @param
     * @return
     */
    @ApiOperationSupport(order = 20)
    @Operation(summary = "资讯列表")
    @GetMapping("/list")
    public Result list(NoticeListQuery noticeListQuery){
        System.out.println("noticeListQuery = " + noticeListQuery);
        List<NoticeListVO> noticeVOS = noticeMapper.selectNoticeByTitleAndType(noticeListQuery);
        log.debug("你的浮木" + (noticeVOS.get(0) instanceof NoticeListVO) + "");
        return Result.ok(noticeVOS);
    }

    /**
     * 3.资讯详情功能
     * @param id
     * @return
     */
    @ApiOperationSupport(order = 30)
    @Operation(summary = "资讯详情")
    @GetMapping("/detail")
    @Parameters(value = {
            @Parameter(name = "id", description = "资讯Id", required = true),
            @Parameter(name = "id", description = "用户名", required = true,hidden = true)})
    public Result detail(Integer id, String username){
        //此处username参数无其他作用，只是为了掩饰Knife4j中参数描述
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.selectDetailById(id);
        return Result.ok(noticeDetailInfoVO);
    }

    /**
     * 4.资讯删除功能
     */
    @ApiOperationSupport(order = 40)
    @Operation(summary = "删除资讯")
    @DeleteMapping("/del")
    public Result delete(Long id){
        int i  = noticeMapper.deleteNoticeById(id);
        if (i > 0){
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 5.资讯更新功能
     */
    @ApiOperationSupport(order = 50)
    @Operation(summary = "更新资讯")
    @PutMapping("/update")
    public Result update(@RequestBody NoticeUpdateParam noticeUpdateParam){
        System.out.println("noticeUpdateParam" + noticeUpdateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam, notice);
        notice.setUpdateTime(new Date());
        System.out.println("notice" + notice);
        int i = noticeMapper.updateNoticeById(notice);
        if (i > 0){
            return Result.ok();
        }
        return Result.fail();
    }
}
